#include"stack.h" 
#define  _CRT_SECURE_NO_WARNINGS 1

bool ifempty(STnode* pst)
{
	assert(pst);
	return pst->top == 0;
}

void Initstack(STnode* pst)
{
	assert(pst);
	pst->data = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

void STpush(STnode* pst,Stadatatype x)
{
	if (pst->capacity == pst->top)
	{
		int newCapicity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		Stadatatype* tem = (Stadatatype*)realloc(pst->data, newCapicity * sizeof(Stadatatype));
		if (tem == NULL)
		{
			perror("malloc error");
			return;
		}
		pst->data = tem;
		pst->capacity = newCapicity;


	}
	pst->data[pst->top] = x;
	pst->top++;
}

void STpop(STnode* pst)
{
	assert(!(ifempty(pst)));
	assert(pst);
	pst->top--;
}


Stadatatype STtake(STnode* pst)
{
	assert(!ifempty(pst));
	return pst -> data[pst->top - 1];
}

void Destroy(STnode* pst)
{
	free(pst->data);
	pst->data = NULL;
	pst->top = pst->capacity = 0;

}

void Print(STnode* pst)
{
	assert(pst);
	int i = 0;
	while (i <= pst->top - 1)
	{
		printf("%d ", pst->data[i++]);
	}
	printf("\n");
	return 0;
}